﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace AtmTransaction
{
    class Withdraw
    {
        public const string CONECTION_STRING = "Server=.\\SQLEXPRESS;" +
                             " Database=ATM; Integrated Security=true";

        public static void AtmWithdraw(int AccountNumber, string pin, decimal amount ) 
        {
            SqlConnection dbCon = new SqlConnection(CONECTION_STRING);
            dbCon.Open();
            if (pin.Length != 4) 
            {
                throw new ArgumentOutOfRangeException("Invalid Pin size");
            }
            using (dbCon)
            {
                SqlTransaction trans = dbCon.BeginTransaction(IsolationLevel.ReadCommitted);
                SqlCommand cmd = dbCon.CreateCommand();
                cmd.Transaction = trans;
                try
                {
                    cmd.CommandText = "SELECT CardNumber , CardPIN, CardCash "+ 
                        "FROM CardAccounts " +
                        "Where CardNumber = @AccountNumber AND CardPIN=@pin AND CardCash>=@amount ";
                    cmd.Parameters.Add("@AccountNumber", AccountNumber);
                    cmd.Parameters.Add("@pin", pin);
                    cmd.Parameters.Add("@amount", amount);
                    SqlDataReader  reader= cmd.ExecuteReader();
                    using (reader)
                    {
                        if (reader.Read() == null)
                        {
                            throw new ArgumentOutOfRangeException("Invalid Account Number");
                        }
                        //else
                        //{
                        //    if ((string)reader["CardPIN"] != pin)
                        //    {
                        //        throw new ArgumentOutOfRangeException("Invalid Pin: ", pin);
                        //    }
                        //    if ((decimal)reader["CardCash"] < amount)
                        //    {
                        //        throw new ArgumentOutOfRangeException("Invalid Pin: ", pin);
                        //    }
                        //}
                        decimal accountSum = (decimal)reader["CardCash"];          
                        accountSum -= amount;
                        cmd.Parameters.Add("@accountSum", accountSum);
                        cmd.CommandText = "UPDATE CardAccounts " +
                            "SET CardCash = @accountSum " +
                            "Where CardNumber = @AccountNumber";
                    }
                    cmd.ExecuteNonQuery();
                    trans.Commit();
                    Console.WriteLine("Transaction Successfull");
                    
                }
                catch
                {
                   trans.Rollback();
                   Console.WriteLine("Transaction Fail");
                }

            }
        }

        static void Main(string[] args)
        {
            AtmWithdraw(321, "rewq", 200);
            AtmWithdraw(222, "rewq", 200);
            AtmWithdraw(222, "xzaq", 200);
        }
    }
}
